﻿using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zj.HttpCodeDemo.Domain.Entities;

namespace Zj.HttpCodeDemo.Repository.Models.Mappings
{
    /// <summary>
    /// Conceptual model news fluent api config.
    /// </summary>
    public class NewsMap
        : EntityTypeConfiguration<News>
    {
        public NewsMap()
        {
            //table
            this.ToTable("News", "dbo");

            //keys
            HasKey(t => t.Id);

            //properties
            this.Property(t => t.Id)
                .HasColumnName("Id")
                .IsRequired();
            this.Property(t => t.Title)
                .HasColumnName("Title")
                .HasMaxLength(30)
                .IsUnicode(true)
                .IsRequired();
            this.Property(t => t.Keyword)
                .HasColumnName("Keyword")
                .HasMaxLength(5)
                .IsUnicode(true)
                .IsRequired();
            this.Property(t => t.Description)
                .HasColumnName("Description")
                .HasMaxLength(255)
                .IsUnicode(true)
                .IsRequired();
            this.Property(t => t.Content)
                .HasColumnName("Content")
                .HasMaxLength(null)
                .IsUnicode(true)
                .IsRequired();
            this.Property(t => t.RowVersion)
                .HasColumnName("RowVersion")
                .IsRowVersion()
                .IsRequired();

            //relations
            this.HasMany(t => t.Tags)
                .WithMany(t => t.Newss)
                .Map(t =>
                {
                    t.ToTable("NewsTag");
                    t.MapLeftKey("NewsId");
                    t.MapRightKey("TagId");
                });
            this.HasRequired(t => t.Category)
                .WithMany(t => t.Newss);
            this.HasRequired(t => t.User)
                .WithMany(t => t.Newss);
        }
    }
}
